package com.alibaba.controller;

import com.alibaba.domain.User;
import com.alibaba.service.UserService;
import com.alibaba.service.impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserController extends HttpServlet {
    private UserService service = new UserServiceImpl();

    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("findAll".equals(method)) {
            findAll(req, resp);
        }
        if ("save".equals(method)) {
            save(req, resp);
        }
        if ("deleteById".equals(method)) {
            deleteByIds(req, resp);
        }
        if ("findById".equals(method)) {
            findById(req, resp);
        }
        if ("update".equals(method)) {
            update(req, resp);
        }
    }

    // TODO 2020年7月16日09:22:30  修改用户
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader = req.getReader();
        //获取前端数据,利用gson第三方工具,将json数据转换成pojo对象
        Gson gson = new Gson();
        User user = gson.fromJson(reader, User.class);

        //调用service,修改用户
        service.update(user);
    }

    // TODO: 2020年7月16日08:59:57  根据id查询用户
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面传递过来的用户id
        String uid = req.getParameter("uid");
        //System.out.println(uid);
        //调用service,查询用户
        User user = service.findById(uid);

        //利用第三方工具将user转成json对象,传递给前端
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(user);

        resp.getWriter().write(json);
    }

    // TODO: 2020年7月15日21:55:27  删除用户
    protected void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader = req.getReader();
        //获取前端数据,利用gson第三方工具,将json数据转换成pojo对象
        Gson gson = new Gson();
        Integer[] ids = gson.fromJson(reader, Integer[].class);
        //  System.out.println(ids[0]);
        //调用service删除
        service.deleteByIds(ids);
    }

    // TODO: 2020年7月15日21:55:27  查询所有用户信息
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用service方法查询所有用户信息
        List<User> userList = service.findAll();

        //System.out.println(userList);
        //将数据已json格式传回页面
        ObjectMapper om = new ObjectMapper();
        String json = om.writeValueAsString(userList);

        resp.getWriter().write(json);
    }

    // TODO: 2020年7月15日21:55:27  //新增用户
    protected void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader = req.getReader();
        //获取前端数据,利用gson第三方工具,将json数据转换成pojo对象
        Gson gson = new Gson();
        User user = gson.fromJson(reader, User.class);

        //调用service,添加用户
        int i = service.save(user);
        // System.out.println(i + "  ::========   111111111111111111111");
        String json = i + "";

        resp.getWriter().write(json);

    }
}
